package com.github.baichuan.web.dto;

import com.github.baichuan.web.data.TimeFormat;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.Optional;

@Getter
@Valid
public class DateTimeRange {
    @Pattern(regexp = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}", message = "日期时间格式应为yyyy-MM-dd HH:mm:ss")
    private String begin;

    @Pattern(regexp = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}", message = "日期时间格式应为yyyy-MM-dd HH:mm:ss")
    private String end;

    public Optional<LocalDateTime> beginDateTime(){
        if(StringUtils.isBlank(begin)){
            return Optional.empty();
        } else {
            return Optional.of(LocalDateTime.parse(begin, TimeFormat.LOCAL_DATE_TIME));
        }
    }

    public Optional<LocalDateTime> endDateTime(){
        if(StringUtils.isBlank(end)){
            return Optional.empty();
        } else {
            return Optional.of(LocalDateTime.parse(end, TimeFormat.LOCAL_DATE_TIME));
        }
    }
}
